<template>
	<el-dialog :title="form.postId ? $t('common.editBtn') : $t('common.addBtn')" width="600" v-model="visible" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" label-width="120px" v-loading="loading">
			<el-form-item label="产品分类名称" prop="categoryName">
				<el-input v-model="form.categoryName" placeholder="请输入 产品分类名称" clearable />
			</el-form-item>
			<el-form-item label="产品分类描述" prop="categoryDesc">
				<el-input type="textarea" maxlength="150" v-model="form.categoryDesc" placeholder="请输入 产品分类描述" clearable />
			</el-form-item>
			<el-form-item label="排序" prop="sortOrder">
				<el-input-number v-model="form.sortOrder" placeholder="请输入 排序" clearable />
			</el-form-item>
			<el-form-item label="产品图片" prop="photo">
				<upload-img v-model:imageUrl="form.photo" width="120px" height="120px">
					<template #empty>
						<el-icon class="add-icon"><Plus /></el-icon>
						<span>请上传产品分类图片</span>
					</template>
				</upload-img>
			</el-form-item>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">{{ $t('common.cancelButtonText') }}</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">{{ $t('common.confirmButtonText') }}</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="systemPostDialog">
import { useMessage } from '/@/hooks/message';
import { getObj, addObj, putObj, validateExist } from '/@/api/qytest/sysProductCategory';
import { useI18n } from 'vue-i18n';
import { rule } from '/@/utils/validate';
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
const { t } = useI18n();
// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);

// 提交表单数据
const form = reactive({
	id: '',
	sortOrder: 0,
	categoryName: '',
	categoryDesc: '',
	photo: '',
});

// 定义校验规则
const dataRules = ref({
	categoryName: [
		{ validator: rule.overLength, trigger: 'blur' },
		{ required: true, message: '产品分类名称不能为空', trigger: 'blur' },
	],
	sortOrder: [
		{ validator: rule.overLength, trigger: 'blur' },
		{ required: true, message: '产品分类排序不能为空', trigger: 'blur' },
	],
});

// 打开弹窗
const openDialog = (id: string) => {
	visible.value = true;
	form.id = '';

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

	// 获取sysProductCategory信息
	if (id) {
		form.id = id;
		getSysProductCategoryData(id);
	}
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(form.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 初始化表单数据
const getSysProductCategoryData = (id: string) => {
	// 获取数据
	loading.value = true;
	getObj({ id: id })
		.then((res: any) => {
			Object.assign(form, res.data[0]);
		})
		.finally(() => {
			loading.value = false;
		});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
